/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package taikai.tree;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.geom.RoundRectangle2D.Double;
import java.io.Serializable;
import javax.swing.JFrame;

public class Tree extends Canvas implements Serializable {

    private final Name[] names = new Name[]{
        new Name(), new Name(), new Name(),
        new Name(), new Name(), new Name(),
        new Name(), new Name()
    };
    private int Name_height;
    private int Name_width;
    private boolean debug = false;

    @Override
    public void paint(Graphics gg) {
        Graphics2D g = (Graphics2D) gg;
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        int rounds = rounds();

        int playersInRound = names.length;

        Name_height = getHeight() / names.length;
        Name_width = getWidth() / rounds;

        int thisRound = 0;
        while (playersInRound > 0) {
            int thing = getHeight() / playersInRound;

            for (int i = 0; i < playersInRound; i++) {
                final int y = (thing * i) + (thing / 2);
                final int x = ((getWidth() / rounds) * thisRound) + Name_width / 2;
                names[i].setLocation(x, y);
                names[i].paint(g);
            }

            playersInRound = playersInRound / 2;
            thisRound++;
        }
    }

    private int rounds() {
        int i = names.length;
        int c = 0;

        while (i > 0) {
            i = i / 2;
            c++;
        }

        return c;
    }

    class Name {

        private int cx;
        private int cy;
        private final int border = 5;
        private String name = "...";
        private final Double aDouble = new RoundRectangle2D.Double();

        {
            aDouble.archeight = 10;
            aDouble.arcwidth = 10;
        }

        void setLocation(int cx, int cy) {
            this.cx = cx;
            this.cy = cy;
        }

        public void paint(Graphics2D g) {
            if(debug) {
                g.drawLine(cx - 2, cy - 2, cx + 2, cy + 2);
                g.drawLine(cx - 2, cy + 2, cx + 2, cy - 2);
            }

            final int width = Name_width/2 - border ;
            final int height = Name_height/2 - border ;
            aDouble.setFrameFromCenter(cx, cy, cx + width, cy + height);
            g.draw(aDouble);
            final Rectangle2D lineMetrics = g.getFontMetrics().getStringBounds(name, g);
            g.drawString(name, (int)(cx - lineMetrics.getWidth()/2), (int)(cy + lineMetrics.getHeight()/2));
        }
    }

    public static void main(String[] args) {
        Tree tree = new Tree();
        JFrame frame = new JFrame();
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(tree, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
